#include "chatbox.h"

ChatBox::ChatBox(QPixmap headPhoto,QString username,QWidget *parent,bool isSender)
    :QWidget(parent),username(username),headPhoto(headPhoto) {
    
    chatMessage="";
    
    // 创建变换对象
    QTransform transform;
    transform.scale(-1, 1);  // 水平翻转
    // transform.scale(1, -1);  // 垂直翻转（取消注释此行可同时进行垂直翻转）
    
    // // 创建消息容器
    // this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    
    // 创建消息元素
    Label_Head_Icon =new QLabel(this);
    Label_Name =new QLabel(this);
    Label_Info =new QLabel(this);
    Label_Head_Icon->setFixedSize(36, 36);
    // 设置消息内容
    Label_Info->setText(chatMessage);
    Label_Info->setWordWrap(true);
    Label_Info->setMaximumWidth(644);
    Label_Info->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
    Label_Info->setTextInteractionFlags(Qt::TextSelectableByMouse);
    
    if(isSender){
        Label_Info->setStyleSheet(
            "font-family:楷体;"
            "border-radius: 8px;"
            "padding: 4px;"
            "color: white;"
            "font-size: 20px;"
            "background-color:#0099ff;"
            );
    }else{
        Label_Info->setStyleSheet(
            "font-family:楷体;"
            "border-radius: 8px;"
            "padding: 4px;"
            "color: #000000;"
            "font-size: 20px;"
            "background-color:#ffffff;"
            );
    }
    
    // 设置头像
    if(!isSender){
        // 应用变换并缩放
        QPixmap transformedPixmap = headPhoto.transformed(transform, Qt::SmoothTransformation);
        transformedPixmap = transformedPixmap.scaled(36, 36, Qt::KeepAspectRatio, Qt::SmoothTransformation);
        // 更新标签显示
        Label_Head_Icon->setPixmap(transformedPixmap);
        
    }else{
        headPhoto=headPhoto.scaled(36, 36, Qt::KeepAspectRatio, Qt::SmoothTransformation);
        // 更新标签显示
        Label_Head_Icon->setPixmap(headPhoto);
    }
    
    
    // 设置昵称
    Label_Name->setText(username);
    Label_Name->setStyleSheet("font-family:楷体;font-weight: bold; font-size: 16px;");
    if(isSender){
        Label_Name->setAlignment(Qt::AlignRight);
    }else{
        Label_Name->setAlignment(Qt::AlignLeft);
    }
    
    
    // 使用水平布局排列消息元素
    QHBoxLayout *hLayout = new QHBoxLayout(this);
    hLayout->setContentsMargins(5, 5, 5, 5); // 减小边距
    hLayout->setSpacing(5);
    
    // 创建头像布局（垂直）
    QVBoxLayout *avatarLayout = new QVBoxLayout();
    avatarLayout->setAlignment(Qt::AlignTop);  // 确保头像在顶部
    avatarLayout->addWidget(Label_Head_Icon);   // 将头像添加到专门的布局中
    
    // 消息内容区域（垂直布局）
    QVBoxLayout *vLayout = new QVBoxLayout();
    vLayout->setSpacing(2);
    if(isSender){
        // 左侧弹簧（将内容推到右侧）
        hLayout->addStretch();
        
        vLayout->addWidget(Label_Name, 0, Qt::AlignRight);
        vLayout->addWidget(Label_Info, 0, Qt::AlignRight);
        hLayout->addLayout(vLayout);
        // 头像
        hLayout->addLayout(avatarLayout);
    }else{
        vLayout->addWidget(Label_Name, 0, Qt::AlignLeft);
        vLayout->addWidget(Label_Info, 0, Qt::AlignLeft);
        // 头像
        hLayout->addLayout(avatarLayout);
        hLayout->addLayout(vLayout);
        // 右侧弹簧
        hLayout->addStretch();
    }
     
}

ChatBox::~ChatBox()
{
}

void ChatBox::setChatMassage(QString message){
    Label_Info->setText(message);
}

QString ChatBox::getChatMassage(){
    return chatMessage;
}

void ChatBox::setUsername(QString username){
    Label_Name->setText(username);
}

QString ChatBox::getUsername(){
    return username;
}

void ChatBox::setHeadPhoto(QPixmap headPhoto){
    Label_Head_Icon->setPixmap(headPhoto);
}

QPixmap ChatBox::getHeadPhoto(){
    return headPhoto;
}
